#!/bin/bash
if [ $USER != 'root' ];then
echo "$USER is not root "
exit 0
fi
which expect &>/dev/null || yum install expect -y
[ -f `pwd`/remote_operate.sh ] && chmod +x `pwd`/remote_operate.sh
[ -f `pwd`/post_cookie_hostinfo.py ] && chmod +x `pwd`/post_cookie_hostinfo.py
yum install -y openssh-clients
if [ ! -f /root/.ssh/id_dsa.pub ];then
ssh-keygen -t dsa -P '' -f /root/.ssh/id_dsa
fi
cat /root/.ssh/id_dsa.pub >> /root/.ssh/authorized_keys
if [ -f `pwd`/ip.txt ];then
for i in `cat ip.txt`;do 
ip=$(echo "$i"|cut -f1 -d":") 
password=$(echo "$i"|cut -f2 -d":")
if [ -z $password ];then
read -p "`echo "$ip login secret is null"`"
exit 0
fi
expect -c "
spawn scp -p /root/.ssh/id_dsa.pub `pwd`/remote_operate.sh  `pwd`/post_cookie_hostinfo.py `pwd`/url.conf root@$ip:/usr/local/bin/
        expect {
                \"*yes/no*\" {send \"yes\r\"; exp_continue}
                \"*password*\" {send \"$password\r\"; exp_continue}
                \"*Password*\" {send \"$password\r\";}
        }
" || ( echo -e "remote ip $ip is not IPaddress or run failure." && continue )
expect -c "
spawn ssh root@$ip "/usr/local/bin/remote_operate.sh"
        expect {
                \"*yes/no*\" {send \"yes\r\"; exp_continue}
                \"*password*\" {send \"$password\r\"; exp_continue}
                \"*Password*\" {send \"$password\r\";}
        }
" || ( echo -e "remote ip $ip is not IPaddress or run failure." && continue )
done
else
echo 'ip.txt is not exist !'
fi
